//
//  MessageViewController.m
//  Tunnel
//
//  Created by wbb on 2022/3/8.
//

#import "MessageViewController.h"
#import "MessageCell.h"
#import "MessageModel.h"
#import "MJRefresh/MJRefresh.h"
#import "YJXYDetailVC.h"
#import "GZGJDetailVC.h"
#import "JKGJDetailVC.h"
#import "YXJCDetailVC.h"
#import "AQJCDetailVC.h"
#import "SPJKVC.h"
#import "VideoDetailVC.h"
@interface MessageViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArr;
@property (nonatomic) NSInteger  page;
@property (nonatomic, copy) NSString * notifyChannel;
@property (nonatomic, copy) NSString * receiver;
@property (nonatomic, copy) NSString * sendStatus;

@end
@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"消息中心";
    self.view.backgroundColor = [UIColor blueColor];
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle: @"清除未读" style:UIBarButtonItemStylePlain target:self action:@selector(clearMsg)];
    right.tintColor = hexColor(2A2A2A);
    self.navigationItem.rightBarButtonItems   = @[right];
    
    self.dataArr = @[].mutableCopy;
    [self initUI];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    [delegate getUnReadNum];

}
- (void)initUI {
    
    self.view.backgroundColor = hexColor(F6F7F9);
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.bottom.top.offset(0);
    }];
    //    self.tableView.contentInset = UIEdgeInsetsMake(0, 12, 0, 12);
    
    kWEAK_SELF
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf initRequest];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf initRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
}
-(void)initRequest{
    kWEAK_SELF;
    [TYNetWorkTool postRequest:TYGetMsg parameters:@{
        @"notifyChannel":@"APP",
        @"pageNum": @(weakSelf.page),
        @"pageSize": @20
    } successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        // 进入DispatchGroup，表示开始一个任务
        NSLog(@"-------1111-----------");
        [WDGlobal shareInstance].emptyType = TYEmptyMessage;
        if(success) {
            if(self.page == 1){
                [weakSelf.dataArr removeAllObjects];
            }
            
            [weakSelf.dataArr addObjectsFromArray:[NSArray yy_modelArrayWithClass:MessageModel.class json:data[@"data"][@"pageInfo"][@"list"]]];
            [weakSelf.tableView reloadData];
            
        }
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        
    } failureBlock:^(NSString * _Nonnull description) {
        // 进入DispatchGroup，表示开始一个任务
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}


-(void)clearMsg{
    kWEAK_SELF;
    [TYNetWorkTool getRequest:TYGetAllMsgRead parameters:@{} successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        // 进入DispatchGroup，表示开始一个任务
        NSLog(@"-------1111-----------");
        if(success) {
            [weakSelf.tableView.mj_header beginRefreshing];
            AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [delegate getUnReadNum];
        }
        
    } failureBlock:^(NSString * _Nonnull description) {
        // 进入DispatchGroup，表示开始一个任务
        
    }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource ----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    return 108;
//}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 108;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"MessageCell";
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.model = self.dataArr[indexPath.row];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MessageModel * model = self.dataArr[indexPath.row];
    
//    VideoDetailVC *vc = [[VideoDetailVC alloc] init];
//    vc.ID = @"537960";//model.bizId;
//    [self.navigationController pushViewController:vc animated:YES];
//    return;
    model.isRead = true;
    if([model.msgType isEqualToString:@"EMERGENCECREATION"]){
        YJXYDetailVC * vc = [YJXYDetailVC new];
        vc.ID = model.bizId;
        [self.navigationController pushViewController:vc animated:true];
    }else if ([model.msgType isEqualToString:@"PLC_FAULT_ALARMS"]) {
        GZGJDetailVC * vc = [GZGJDetailVC new];
        vc.gjId = model.bizId;
        vc.lx = @"1";
        [self.navigationController pushViewController:vc animated:true];
    }else if ([model.msgType isEqualToString:@"THRESHOLD_MONITORING_ALARMS"]) {
        JKGJDetailVC * vc = [JKGJDetailVC new];
        vc.jkId = model.bizId;
        [self.navigationController pushViewController:vc animated:true];
    }else if ([model.msgType isEqualToString:@"THE_DEVICE_IS_OFFLINE"]) {
        YXJCDetailVC * vc = [YXJCDetailVC new];
        vc.ID = model.bizId;
        [self.navigationController pushViewController:vc animated:true];
    }else if ([model.msgType isEqualToString:@"SECURITYREVIEW"]) {
        AQJCDetailVC * vc = [AQJCDetailVC new];
        vc.ID = model.bizId;
        [self.navigationController pushViewController:vc animated:true];
    }else if ([model.msgType isEqualToString:@"NON_MOTORIZED_VEHICLE_BREAK_IN"] || [model.msgType isEqualToString:@"PEDESTRIAN_TRESPASSING"] || [model.msgType isEqualToString:@"SCATTERED_OBJECTS"] || [model.msgType isEqualToString:@"WORK_CAR"]) {
        
        //视频监控
        VideoDetailVC *vc = [[VideoDetailVC alloc] init];
        vc.isShowHistory = NO;
        vc.isHeight = NO;
        vc.ID = model.bizId;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([model.msgType isEqualToString:@"MULTI_VEHICLE_ACCIDENTS"] || [model.msgType isEqualToString:@"BICYCLE_PARKING"] || [model.msgType isEqualToString:@"RETROGRADE"] || [model.msgType isEqualToString:@"LANE_CHANGES_IN_VIOLATION_OF_REGULATIONS"] || [model.msgType isEqualToString:@"CRIMPING"] || [model.msgType isEqualToString:@"OCCUPY_A_DEDICATED_LANE"] || [model.msgType isEqualToString:@"TRAFFIC_CONGESTION"] ){
        
        //视频监控
        VideoDetailVC *vc = [[VideoDetailVC alloc] init];
        vc.isShowHistory = YES;
        vc.isHeight = NO;
        vc.ID = model.bizId;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([model.msgType isEqualToString:@"FOCUS_ON_VEHICLES"] ){// 超高
        //视频监控
        
        VideoDetailVC *vc = [[VideoDetailVC alloc] init];
        vc.ID = model.bizId;
        vc.isShowHistory = YES;
        vc.isHeight = YES;
        vc.eventType = model.bizType;
        [self.navigationController pushViewController:vc animated:YES];
    }
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    kWEAK_SELF;
    [TYNetWorkTool getRequest:[NSString stringWithFormat:@"%@%@",TYGetMsgRead,model.ID] parameters:@{} successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        // 进入DispatchGroup，表示开始一个任务
        NSLog(@"-------1111-----------");
        if(success) {
            // 进详情
            
        }
        
    } failureBlock:^(NSString * _Nonnull description) {
        // 进入DispatchGroup，表示开始一个任务
        
    }];
    
}


#pragma mark ---- lazy ----
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
@end

